#提交/判题模块
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from database import get_db
from models import Submission
from schemas import SubmissionCreate, SubmissionOut
from typing import List
from routers.users import get_current_user  # 用于鉴权

router = APIRouter()

# 学生提交代码
@router.post("/", response_model=SubmissionOut)
def create_submission(
    submission: SubmissionCreate,
    db: Session = Depends(get_db),
    current_user: dict = Depends(get_current_user)  # 获取登录用户
):
    # 默认所有登录用户都是学生，后续可以检查角色
    db_submission = Submission(
        homework_id=submission.homework_id,
        problem_id=submission.problem_id,
        code=submission.code,
        student_id=current_user["id"],  # 从 token 获取学生 ID
        status="Pending"
    )
    db.add(db_submission)
    db.commit()
    db.refresh(db_submission)
    return db_submission


# 查看某个作业的所有提交
@router.get("/homework/{homework_id}", response_model=List[SubmissionOut])
def get_submissions_by_homework(homework_id: int, db: Session = Depends(get_db)):
    return db.query(Submission).filter(Submission.homework_id == homework_id).all()


# 查看某个学生的所有提交
@router.get("/student/{student_id}", response_model=List[SubmissionOut])
def get_submissions_by_student(student_id: int, db: Session = Depends(get_db)):
    return db.query(Submission).filter(Submission.student_id == student_id).all()
